<?php
require_once dirname(__FILE__) . "/../../includes/config.php";
require_once PHP_ROOTPATH.'/classes/database.php';
//récupération des informations de la biobanque et des commandes en fonction du tissu et de l'animal sélectionné
header("Content-Type: text/xml; charset=utf-8");
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
echo "<list>\n";
 
$type_tissu = (isset($_POST["type_tissu"])) ? html_entity_decode($_POST["type_tissu"]) : NULL;
$id_anim = (isset($_POST["id"])) ? html_entity_decode($_POST["id"]) : NULL;

if ($type_tissu && $id_anim) {
    $id_connect=Database::getInstance();
    $query_temp = "SELECT * FROM Biobanque WHERE type_tissu LIKE '%".pg_escape_string($type_tissu) ."%' AND id_animal='".$id_anim."'";
    $res_query = pg_query($id_connect , $query_temp) or die(pg_last_error());
    
    while ($back = pg_fetch_assoc($res_query)) {
		echo "<item id=\"Informations\" />\n";
        echo "<item id=\"Type de tissu\" name=\"" . $back["type_tissu"] . "\" />\n";
        echo "<item id=\"Nom de la boite\" name=\"" . $back["nom_boite"] . "\" />\n";
        if($back["localisation"]){ echo "<item id=\"Localisation\" name=\"" . $back["localisation"] . "\" />\n"; }
        $unite = ($back["unite"]) ? $back["unite"]  : "";
        $qte_restante= 0;
        if($back["qte_initiale"]){ 
        	$qte_restante=$back["qte_initiale"];
        	echo "<item id=\"quantité initiale\" name=\"" . $back["qte_initiale"] . " ". $unite. "\" />\n"; 
        }
		
    
		$query_temp2 = "SELECT * FROM Tissu_fixe WHERE id_biobanque='".$back["id_biobanque"]."'";
		$res_query2 = pg_query($id_connect , $query_temp2) or die(pg_last_error());
		while ($back2 = pg_fetch_assoc($res_query2)) {
			if($back2["date_pbs_azide"]){echo "<item id=\"Date de fixation\" name=\"" . $back2["date_pbs_azide"] . "\" />\n"; }
		}
		
		$query_temp3 = "SELECT * FROM Commande WHERE id_biobanque='".$back["id_biobanque"]."'";
		$res_query3 = pg_query($id_connect , $query_temp3) or die(pg_last_error());
		while ($back3 = pg_fetch_assoc($res_query3)) {
			if($back3["num_commande"]){
				echo "<item id=\"Commande\" />\n";
				echo "<item id=\"Numéro de commande\" name=\"" . $back3["num_commande"] . "\" />\n"; 
			}
			if($back3["date_envoi"]){echo "<item id=\"Date d'envoi\" name=\"" . date("d/m/Y", strtotime($back3["date_envoi"])) . "\" />\n"; }
			if($back3["qte_envoyee"]){
				if ($qte_restante > 0)
					$qte_restante -= $back3["qte_envoyee"];
				echo "<item id=\"Quantité envoyée\" name=\"" . $back3["qte_envoyee"] . " ". $unite . "\" />\n"; 
			}
			if($back3["nom_labo"]){echo "<item id=\"Nom du laboratoire\" name=\"" . $back3["nom_labo"] . "\" />\n"; }
		}
		
		echo '<item id="Quantité restante"/>'."\n";
		echo '<item id="Quantité" name="'.$qte_restante. " ". $unite.'"/>'."\n";
    }
}
 
echo "</list>";


?>
